//Written by Paul Stothard, University of Alberta, Canada
//Revised and redistributed by Ting-Kai Liu, Instite of Hydrobiology, Chinese Academy of Sciences, Wuhan, China.

function Base32CodesToIUPACCodes (theDocument) {

    	var newDna = "";
    	var maxInput = 500000;

	if (testScript() == false) {
		return false;
	}

   if ((checkFormElement (theDocument.forms[0].elements[0]) == false) || (checkTextLength(theDocument.forms[0].elements[0].value, maxInput) == false))	{
		return false;
   }	        
	
   newDna = theDocument.forms[0].elements[0].value.replace(/[^0-9a-hjkmnp-z]/g,"");
	newDna = newDna.toUpperCase();
	newDna = newDna.replace(/1/g,"A");
	newDna = newDna.replace(/2/g,"C");
	newDna = newDna.replace(/3/g,"M");
	newDna = newDna.replace(/4/g,"G");
	newDna = newDna.replace(/5/g,"R");
	newDna = newDna.replace(/6/g,"S");
	newDna = newDna.replace(/7/g,"V");
	newDna = newDna.replace(/8/g,"T");
	newDna = newDna.replace(/9/g,"W");
	newDna = newDna.replace(/a/g,"Y");
	newDna = newDna.replace(/b/g,"H");
	newDna = newDna.replace(/c/g,"K");
	newDna = newDna.replace(/d/g,"K");
	newDna = newDna.replace(/e/g,"B");
	newDna = newDna.replace(/f/g,"N");
	newDna = newDna.replace(/g/g,"-");
	newDna = newDna.replace(/[hjkmnp-z]/g,"X");
	newDna = newDna.toUpperCase();					
	openWindow("IUPAC codes to Base-32 codes");
	openPre();
	outputWindow.document.write ('&gt;filtered DNA sequence consisting of ' + newDna.length + ' bases.\n');
	outputWindow.document.write (addReturns(newDna));
	outputWindow.document.write ("\n");	
	closePre();
	closeWindow();
    	return true;
}
